package m.jclecai.com.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;
import java.util.HashMap;

import m.jclecai.com.MyApplication;
import m.jclecai.com.R;
import m.jclecai.com.bean.IssueInfoBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.inter.OnModificationSchemeListener;
import m.jclecai.com.inter.OnResultCallback;
import m.jclecai.com.utils.OkHttpClientManager;
import m.jclecai.com.utils.ToastUtil;
import m.jclecai.com.utils.http.FastOk;

/**
 * Created by hp on 2017/4/17.
 */

public class ModificationSchemeDialog extends Dialog implements View.OnClickListener, TextWatcher {
    private final OnModificationSchemeListener listener;
    private final String type;
    private final Context context;
    //每期追号
    private EditText et_count;
    //连续追号
    private EditText et_count_continuous;
    //全程最低盈利
    private EditText et_profit;
    //全程最低盈利率
    private EditText et_profit_percent;
    //前多少期盈利
    private EditText et_profit_count;
    //之前盈利
    private EditText et_profit_before;
    //之后盈利
    private EditText et_profit_after;
    //前多少期盈利率
    private EditText et_percent_count;
    //之前盈利率
    private EditText et_percent_before;
    //之后盈利率
    private EditText et_percent_after;

    private CheckBox[] cheboxs;
    private IssueInfoBean bean;
    //方案条目
    private int item;
    private HashMap<String, String> schemeInfo = new HashMap<>();
    private MyApplication app;
    private ProgressDialog dialog;


    public ModificationSchemeDialog(Context context, String type, OnModificationSchemeListener listener) {
        super(context, R.style.ProgressStyle);
        this.listener = listener;
        this.type = type;
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modification_scheme);
        app = (MyApplication) context.getApplicationContext();
        init();
    }

    private void init() {
        initView();
        initData();

    }

    private void initData() {
    }

    private void initView() {
        TextView tv_cancel = (TextView) findViewById(R.id.tv_cance);
        tv_cancel.setOnClickListener(this);
        TextView tv_confirm = (TextView) findViewById(R.id.tv_confirm);
        tv_confirm.setOnClickListener(this);

        et_count = (EditText) findViewById(R.id.et_count);
        et_count.addTextChangedListener(this);
        TextView tv_add = (TextView) findViewById(R.id.tv_add);
        tv_add.setOnClickListener(this);
        TextView tv_deduct = (TextView) findViewById(R.id.tv_deduct);
        tv_deduct.setOnClickListener(this);

        et_count_continuous = (EditText) findViewById(R.id.et_count_continuous);
        et_count_continuous.addTextChangedListener(this);
        TextView tv_add_continuous = (TextView) findViewById(R.id.tv_add_continuous);
        tv_add_continuous.setOnClickListener(this);
        TextView tv_deduct_continuous = (TextView) findViewById(R.id.tv_deduct_continuous);
        tv_deduct_continuous.setOnClickListener(this);

        CheckBox cb_one = (CheckBox) findViewById(R.id.cb_one);
        cb_one.setOnClickListener(this);
        CheckBox cb_two = (CheckBox) findViewById(R.id.cb_two);
        cb_two.setOnClickListener(this);
        CheckBox cb_three = (CheckBox) findViewById(R.id.cb_three);
        cb_three.setOnClickListener(this);
        CheckBox cb_four = (CheckBox) findViewById(R.id.cb_four);
        cb_four.setOnClickListener(this);

        cheboxs = new CheckBox[]{cb_one, cb_two, cb_three, cb_four};

        //最低盈利
        et_profit = (EditText) findViewById(R.id.et_profit);
        et_profit.addTextChangedListener(this);
        //最低盈利率
        et_profit_percent = (EditText) findViewById(R.id.et_profit_percent);
        et_profit_percent.addTextChangedListener(this);

        //前多少期
        et_profit_count = (EditText) findViewById(R.id.et_profit_count);
        et_profit_count.addTextChangedListener(this);
        //之前盈利
        et_profit_before = (EditText) findViewById(R.id.et_profit_before);
        et_profit_before.addTextChangedListener(this);
        //之后盈利
        et_profit_after = (EditText) findViewById(R.id.et_profit_after);
        et_profit_after.addTextChangedListener(this);

        //前多少期
        et_percent_count = (EditText) findViewById(R.id.et_percent_count);
        et_percent_count.addTextChangedListener(this);
        //之前盈利率
        et_percent_before = (EditText) findViewById(R.id.et_percent_before);
        et_percent_before.addTextChangedListener(this);
        //之后盈利率
        et_percent_after = (EditText) findViewById(R.id.et_percent_after);
        et_percent_after.addTextChangedListener(this);
        getSaveSchemeInfo();
    }


    //获取方案信息
    private void getSaveSchemeInfo() {
        if (!app.isNull) {
            et_count_continuous.setText(app.getCountContinuous());
            et_count.setText(app.getCount());

            et_profit.setText(app.getProfit());
            et_profit_percent.setText(app.getProfitPercent());

            et_profit_count.setText(app.getProfitCount());
            et_profit_before.setText(app.getProfitBefore());
            et_profit_after.setText(app.getProfitAfter());


            et_percent_count.setText(app.getPercentCount());
            et_percent_before.setText(app.getPercentBefore());
            et_percent_after.setText(app.getPercentAfter());
            cheboxs[app.getItem()].setChecked(true);
        } else {
            cheboxs[0].setChecked(true);
        }
    }


    private void getSchemeInfo(String term) {
        String count = et_count.getText().toString();
        String countContinuous = et_count_continuous.getText().toString();

        //每期倍数
        if (count.isEmpty()) {
            schemeInfo.put("count", "1");
            app.setCount("1");
        } else {
            schemeInfo.put("count", count);
            app.setCount(count);
        }

        //连续追号期数
        if (countContinuous.isEmpty()) {
            schemeInfo.put("countContinuous", "10");
            app.setCountContinuous("10");
        } else {
            int i = Integer.parseInt(countContinuous);
            int surplus = 0;
            try{
                surplus = 87 - Integer.parseInt(term);
            }catch (Exception e){
                e.printStackTrace();
            }
            if (i > surplus) {
                ToastUtil.showToast(context, "剩余最大期数" + surplus);
                et_count_continuous.setText(String.valueOf(surplus));
            }
            schemeInfo.put("countContinuous", et_count_continuous.getText().toString());
            app.setCountContinuous(et_count_continuous.getText().toString());
        }

        switch (item) {
            case 0:
                String profit = et_profit.getText().toString();
                if (profit.isEmpty()) {
                    app.setProfit("30");
                    schemeInfo.put("profit", "30");
                } else {
                    app.setProfit(profit);
                    schemeInfo.put("profit", profit);
                }
                break;
            case 1:
                String profitPercent = et_profit_percent.getText().toString();
                if (profitPercent.isEmpty()) {
                    app.setProfitPercent("30");
                    schemeInfo.put("profitPercent", "30");
                } else {
                    app.setProfitPercent(profitPercent);
                    schemeInfo.put("profitPercent", profitPercent);
                }
                break;
            case 2:
                String percentCount = et_percent_count.getText().toString();
                String percentBefore = et_percent_before.getText().toString();
                String percentAfter = et_percent_after.getText().toString();
                if (percentCount.isEmpty()) {
                    app.setPercentCount("5");
                    schemeInfo.put("percentCount", "5");
                } else {
                    int i = Integer.parseInt(percentCount);
                    int surplus = 87 - Integer.parseInt(term);
                    if (i > surplus) {
                        ToastUtil.showToast(context, "剩余最大期数" + surplus);
                        et_percent_count.setText(String.valueOf(surplus));
                    }
                    app.setPercentCount(et_percent_count.getText().toString());
                    schemeInfo.put("percentCount", et_percent_count.getText().toString());
                }

                if (percentBefore.isEmpty()) {
                    app.setPercentBefore("30");
                    schemeInfo.put("percentBefore", "30");
                } else {
                    app.setPercentBefore(percentBefore);
                    schemeInfo.put("percentBefore", percentBefore);
                }

                if (percentAfter.isEmpty()) {
                    app.setPercentAfter("30");
                    schemeInfo.put("percentAfter", "30");
                } else {
                    app.setPercentAfter(percentAfter);
                    schemeInfo.put("percentAfter", percentAfter);
                }
                break;
            case 3:
                String profitCount = et_profit_count.getText().toString();
                String profitBefore = et_profit_before.getText().toString();
                String profitAfter = et_profit_after.getText().toString();
                if (profitCount.isEmpty()) {
                    app.setProfitCount("5");
                    schemeInfo.put("profitCount", "5");
                } else {
                    int i = Integer.parseInt(profitCount);
                    //获取剩余期数
                    int surplus = 87 - Integer.parseInt(term);
                    if (i > surplus) {
                        ToastUtil.showToast(context, "剩余最大期数" + surplus);
                        et_profit_count.setText(String.valueOf(surplus));
                    }
                    app.setProfitCount(et_profit_count.getText().toString());
                    schemeInfo.put("profitCount", et_profit_count.getText().toString());
                }

                if (profitBefore.isEmpty()) {
                    schemeInfo.put("profitBefore", "30");
                    app.setProfitBefore("30");
                } else {
                    app.setProfitBefore(profitBefore);
                    schemeInfo.put("profitBefore", profitBefore);
                }

                if (profitAfter.isEmpty()) {
                    schemeInfo.put("profitAfter", "30");
                    app.setProfitAfter("30");
                } else {
                    app.setProfitAfter(profitAfter);
                    schemeInfo.put("profitAfter", profitAfter);
                }
                break;
        }
        //方案item
        schemeInfo.put("item", String.valueOf(item));
        app.setItem(item);
        app.isNull = false;
        listener.onConfirmListener(schemeInfo);
        this.dismiss();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cance:
                listener.onCancelListener();
                this.dismiss();
                break;
            case R.id.tv_confirm:
                getIssueInfo(type);
                break;
            case R.id.tv_add:
                addAndDeduct(et_count, true);
                break;
            case R.id.tv_deduct:
                addAndDeduct(et_count, false);
                break;
            case R.id.tv_add_continuous:
                addAndDeduct(et_count_continuous, true);
                break;
            case R.id.tv_deduct_continuous:
                addAndDeduct(et_count_continuous, false);
                break;
            case R.id.cb_one:
                setCheboxs(0);
                item = 0;
                break;
            case R.id.cb_two:
                setCheboxs(1);
                item = 1;
                break;
            case R.id.cb_three:
                setCheboxs(2);
                item = 2;
                break;
            case R.id.cb_four:
                setCheboxs(3);
                item = 3;
                break;
        }
    }

    int defaultNumber = 10;

    private void addAndDeduct(EditText et, boolean isAdd) {
        String s = et.getText().toString();
        if (isAdd) {
            if (s.isEmpty()) {
                int number = defaultNumber + 1;
                et.setText(String.valueOf(number));
            } else {
                int i = Integer.parseInt(s);
                int number = i + 1;
                et.setText(String.valueOf(number));
            }
        } else {
            if (s.isEmpty()) {
                int number = defaultNumber - 1;
                et.setText(String.valueOf(number));
            } else {
                int i = Integer.parseInt(s);
                int number = i - 1;
                et.setText(String.valueOf(number));
            }
        }
    }


    private void setCheboxs(int position) {
        for (int i = 0; i < cheboxs.length; i++) {
            if (i == position) {
            } else {
                cheboxs[i].setChecked(false);
            }
        }
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {


    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        //连续追号
        String s1 = et_count_continuous.getText().toString();
        //防止0开头数字
        if (!s1.isEmpty()) {
            int len = s1.length();
            if (len == 1 && s1.equals("0")) {
                s.clear();
            }
        }

        //每期追号倍数
        String s2 = et_count.getText().toString();
        //防止0开头数字
        if (!s2.isEmpty()) {
            int len = s2.length();
            if (len == 1 && s2.equals("0")) {
                s.clear();
            }
            //最大倍数不能超过9999
            int i = Integer.parseInt(s2);
            if (i > 9999) {
                et_count.setText("9999");
            }
        }
        //全程最低盈利
        String s3 = et_profit.getText().toString();
        //防止0开头数字
        if (!s3.isEmpty()) {
            int len = s3.length();
            if (len == 1 && s3.equals("0")) {
                s.clear();
            }
            //最大盈利不能超过99999元
            int i = Integer.parseInt(s3);
            if (i > 99999) {
                et_profit.setText("99999");
            }
        }
        //全程最低盈利率
        String s4 = et_profit_percent.getText().toString();
        if (!s4.isEmpty()) {
            int len = s4.length();
            if (len == 1 && s4.equals("0")) {
                s.clear();
            }
            //最大盈利率不能超过9999%
            int i = Integer.parseInt(s4);
            if (i > 9999) {
                et_profit_percent.setText("9999");
            }
        }
        //前多少期
        String s5 = et_percent_count.getText().toString();
        if (!s5.isEmpty()) {
            int len = s5.length();
            if (len == 1 && s5.equals("0")) {
                s.clear();
            }
        }

        //之前盈利率
        String s6 = et_percent_before.getText().toString();
        if (!s6.isEmpty()) {
            int len = s6.length();
            if (len == 1 && s6.equals("0")) {
                s.clear();
            }
            //最大盈利率不能超过9999
            int i = Integer.parseInt(s6);
            if (i > 9999) {
                et_percent_before.setText("9999");
            }
        }

        //之后盈利率
        String s7 = et_percent_after.getText().toString();
        if (!s7.isEmpty()) {
            int len = s7.length();
            if (len == 1 && s7.equals("0")) {
                s.clear();
            }
            //最大盈利率不能超过9999
            int i = Integer.parseInt(s7);
            if (i > 9999) {
                et_percent_after.setText("9999");
            }
        }


        //前多少期
        String s8 = et_profit_count.getText().toString();
        if (!s8.isEmpty()) {
            int len = s8.length();
            if (len == 1 && s8.equals("0")) {
                s.clear();
            }
        }

        //之前盈利
        String s9 = et_profit_before.getText().toString();
        if (!s9.isEmpty()) {
            int len = s9.length();
            if (len == 1 && s9.equals("0")) {
                s.clear();
            }
            //最大盈利不能超过9999
            int i = Integer.parseInt(s9);
            if (i > 99999) {
                et_profit_before.setText("99999");
            }
        }

        //之后盈利
        String s10 = et_profit_after.getText().toString();
        if (!s10.isEmpty()) {
            int len = s10.length();
            if (len == 1 && s10.equals("0")) {
                s.clear();
            }
            //最大盈利率不能超过9999
            int i = Integer.parseInt(s10);
            if (i > 99999) {
                et_profit_after.setText("99999");
            }
        }
    }


    private void getIssueInfo(String type) {
        OkHttpClientManager.Param Url = new OkHttpClientManager.Param("url", Constant_Url.ISSUE_INFO);
        OkHttpClientManager.Param Type = new OkHttpClientManager.Param("type", type);
        OkHttpClientManager.Param[] param = new OkHttpClientManager.Param[]{Url, Type};
        try {
            OkHttpClientManager.getInstance().asynPost(new OnResultCallback() {
                @Override
                public void onGetResult(String json) {
                    IssueInfoBean bean = FastOk.get(json, IssueInfoBean.class);
                    if(bean != null) {
                        String str = bean.getTermNo();
                        if(!TextUtils.isEmpty(str) && str.length() > 1) {
                            String term = str.substring(str.length() - 2, str.length());
                            getSchemeInfo(term);
                        }
                    }
                }

                @Override
                public void onError() {
                    ToastUtil.showToast(context, R.string.connect_failure);
                }
            }, param);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
